﻿namespace InternetBankService.Helpers
{
    using System;
    using System.Net;
    using System.Net.Mail;
    using System.Configuration;

    public class MailHelper : IMailSender
    {
        public void SendEmail(string toAddress, string subject, string body)
        {
            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587)
                {
                    Credentials = new NetworkCredential(ConfigurationManager.AppSettings["MailAdress"],
                                                        ConfigurationManager.AppSettings["MailPassword"]),
                    EnableSsl = true
                };

            MailMessage mailMessage = new MailMessage
                {
                    From = new MailAddress(ConfigurationManager.AppSettings["MailAdress"]),
                    Subject = subject,
                    Body = body
                };

            mailMessage.To.Add(new MailAddress(toAddress));

            try
            {
                smtpClient.Send(mailMessage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
